package com.groupbuying.customerconsumer.utils.apiresult;

import java.util.HashMap;

/**
 * @anthor zmlmagic
 * @date 2019/1/11 3:12 PM
 * @company 蒙泰互联网公司
 * @des 统一返回
 **/
public class ApiResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /**
     * 编码
     */
    private Integer code;


    /**
     * 消息
     */
    private String msg;


    /**
     * 数据
     */
    private Object result;


    public ApiResult() {

    }


    public ApiResult(Integer code, String msg) {
        this.put("code", code);
        this.put("msg", msg);
    }


    public static ApiResult success() {
        ApiResult result = new ApiResult();
        result.setResultCode(ApiResultCode.SUCCESS);
        return result;
    }

    public static ApiResult success(Object resultData) {
        ApiResult result = new ApiResult();
        result.setResultCode(ApiResultCode.SUCCESS);
        result.setResult(resultData);
        return result;
    }

    public static ApiResult success(ApiResultCode apiResultCode, Object resultData) {
        ApiResult result = new ApiResult();
        result.setResultCode(apiResultCode);
        result.setResult(resultData);
        return result;
    }

    public static ApiResult failure() {
        ApiResult result = new ApiResult();
        result.setResultCode(ApiResultCode.FAILURE);
        return result;
    }

    public static ApiResult failure(Object resultData) {
        ApiResult result = new ApiResult();
        result.setResultCode(ApiResultCode.FAILURE);
        result.setResult(resultData);
        return result;
    }

    public static ApiResult failure(ApiResultCode apiResultCode, Object resultData) {
        ApiResult result = new ApiResult();
        result.setResultCode(apiResultCode);
        result.setResult(resultData);
        return result;
    }

    public static ApiResult failure(Integer code, String message, Object resultData) {
        ApiResult result = new ApiResult();
        result.setCode(code);
        result.setMsg(message);
        result.setResult(resultData);
        return result;
    }

    public static ApiResult operateSuccess() {
        ApiResult result = new ApiResult();
        result.setResultCode(ApiResultCode.OPRSUCCESS);
        return result;
    }

    //检测参数为空
    public static ApiResult paramIsNull(){
        ApiResult result = new ApiResult();
        result.setCode(ApiResultCode.PARAMNULL.code());
        result.setMsg(ApiResultCode.PARAMNULL.message());
        return result;
    }

    public void setResultCode(ApiResultCode code) {
        this.put("code", code.code());
        this.put("msg", code.message());
    }


    public Integer getCode() {
        return (Integer) this.get("code");
    }


    public void setCode(Integer code) {
        this.put("code", code);
    }


    public String getMsg() {
        return this.get("msg").toString();
    }


    public void setMsg(String msg) {
        this.put("msg", msg);
    }


    public Object getResult() {
        return this.get("result");
    }


    public void setResult(Object result) {
        this.put("result", result);
    }
}
